package zzzhc.image.recognize.nn;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class NNOutput implements Serializable {

	private static final long serialVersionUID = -6734574185505156605L;

	private List<NNOutputItem> items = new ArrayList<NNOutputItem>();

	public NNOutput() {

	}

	public List<NNOutputItem> getItems() {
		return items;
	}

	public void addItem(NNOutputItem item) {
		items.add(item);
	}

	public void addItem(double[] data, char code) {
		NNOutputItem item = new NNOutputItem();
		item.setData(data);
		item.setCode(code);
		addItem(item);
	}

	public void addItem(double[] data) {
		NNOutputItem item = new NNOutputItem();
		item.setData(data);
		addItem(item);
	}

	public boolean assignCode(NNInput input) {
		List<NNInputItem> iis = input.getItems();
		int len = items.size()<=iis.size()?items.size():iis.size();
		for (int i = 0; i < len; i++) {
			NNOutputItem item = items.get(i);
			item.setCode(iis.get(i).getCode());
		}
		return true;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("NNOutput[\n");

		for (NNOutputItem item : items) {
			for (double d : item.getData()) {
				sb.append(d).append(";");
			}
			sb.append(item.getCode()).append("\n");
		}

		sb.append("]\n");
		return sb.toString();
	}

	private boolean isOne(double d) {
		return ((int) d) == 1;
	}

	public char getChar(double[] data) {
		for (NNOutputItem item : items) {
			double[] dd = item.getData();
			for (int i = 0; i < dd.length; i++) {
				if (isOne(dd[i]) && isOne(data[i])) {
					return item.getCode();
				}
			}
		}
		return (char) 0;
	}

}
